# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def oddEvenList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        odd = ListNode(0)
        oddCur = odd
        even = ListNode(0)
        evenCur = even

        while head:
            oddCur.next = head
            head = head.next
            oddCur = oddCur.next
            oddCur.next = None
            if not head: break
            evenCur.next = head
            head = head.next
            evenCur = evenCur.next
            evenCur.next = None
        if odd is oddCur:
            return even.next
        elif even is evenCur:
            return odd.next
        else:
            oddCur.next = even.next
            return odd.next
        